﻿using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindows.Models.Homes;
using static NPOI.HSSF.Util.HSSFColor;

namespace UIWindows.ViewModels
{
    public class CPKReportFormViewModel : XingLucifer.IBase.IViewStatus
    {
        public GlobalStatics.HomeStatic Home { set; get; }
        private GlobalStatics.SystemConfigStatic _configStatic;
        private GlobalStatics.ParameterStatic _parameter;
        private GlobalStatics.TemporaryStatic _temporary;
        public CPKReportFormViewModel(GlobalStatics.SystemConfigStatic configStatic, GlobalStatics.TemporaryStatic temporary,
            GlobalStatics.HomeStatic home, GlobalStatics.ParameterStatic parameter)
        {
            _temporary = temporary;
            _configStatic = configStatic;
            Series = new ObservableCollection<ISeries>();
            StationNoList = new ObservableCollection<string>();
            Home = home;
            _parameter = parameter;
            AddSeries();
        }
        public System.Collections.ObjectModel.ObservableCollection<string> StationNoList { get; set; }
        public System.Collections.ObjectModel.ObservableCollection<ISeries> Series { get; set; }
        private string _selectedStationNo;

        public string SelectedStationNo
        {
            get { return _selectedStationNo; }
            set
            {
                _selectedStationNo = value;
                IsFilterDisplay(value);
            }
        }

        private void IsFilterDisplay(string value)
        {
            if (value == "全部")
            {
                foreach (var item in Series)
                {
                    item.IsVisible = true;
                }
                return;
            }
            foreach (var item in Series.Where(x => x.Name != "Upper limit" && x.Name != "Standard" && x.Name != "Lower limit"))
            {
                item.IsVisible = false;
            }
            var model = Series.FirstOrDefault(x => x.Name == value);
            if (model != null)
            {
                model.IsVisible = true;
            }
        }
        private SkiaSharp.SKColor[] sKColors = new SkiaSharp.SKColor[] { SkiaSharp.SKColors.Gold,
            SkiaSharp.SKColors.Black, SkiaSharp.SKColors.Teal, SkiaSharp.SKColors.Aqua,SkiaSharp.SKColors.CadetBlue, SkiaSharp.SKColors.Chocolate, SkiaSharp.SKColors.DarkMagenta,
        SkiaSharp.SKColors.DarkGoldenrod,SkiaSharp.SKColors.DarkGreen,SkiaSharp.SKColors.DarkSalmon};

        public Axis[] XAxes { get; set; } =
        {
            new Axis
            {
                MinLimit = 1,
                MaxLimit = 50,
                ForceStepToMin = true,
                MinStep = 1,
                TextSize = 14,
                //SeparatorsPaint = new LiveChartsCore.SkiaSharpView.Painting.SolidColorPaint
                //{
                //    Color = SkiaSharp.SKColors.Aqua,
                //    StrokeThickness = 2
                //}
            }
        };

        public List<Axis> YAxes { get; set; } = new List<Axis>
        {
            new Axis
            {
                ForceStepToMin = true,
                MinStep = 0.5,
                SeparatorsPaint = new LiveChartsCore.SkiaSharpView.Painting.SolidColorPaint
                {
                    Color = SkiaSharp.SKColors.DarkGray,
                    StrokeThickness = 1
                }
            }
        };
        SkiaSharp.SKColor _red = new SkiaSharp.SKColor(229, 57, 53);
        SkiaSharp.SKColor _gree = new SkiaSharp.SKColor(0x00, 0xEC, 0x00);
        /// <summary>
        /// 上限
        /// </summary>
        private ObservableCollection<double> UpperLimit { get; set; }
        /// <summary>
        /// 标准值
        /// </summary>
        private ObservableCollection<double> StandardValue { get; set; }
        /// <summary>
        /// 下限
        /// </summary>
        private ObservableCollection<double> LowerLimit { get; set; }
        private void AddSeries()
        {
            StationNoList.Clear();
            StationNoList.Add("全部");
            UpperLimit = new ObservableCollection<double>();
            StandardValue = new ObservableCollection<double>();
            LowerLimit = new ObservableCollection<double>();
            var _lineSeries = new LineSeries<double>
            {
                Values = UpperLimit,
                Stroke = new LiveChartsCore.SkiaSharpView.Painting.SolidColorPaint(_red, 2),
                GeometryStroke = new LiveChartsCore.SkiaSharpView.Painting.SolidColorPaint(_red, 2),
                LineSmoothness = 0,
                GeometrySize = 8,
                Fill = null,
                Name = "Upper limit",
                
            };
            var _lineSeries1 = new LineSeries<double>
            {
                Values = StandardValue,
                Stroke = new LiveChartsCore.SkiaSharpView.Painting.SolidColorPaint(_gree, 2),
                GeometryStroke = new LiveChartsCore.SkiaSharpView.Painting.SolidColorPaint(_gree, 2),
                LineSmoothness = 0,
                GeometrySize = 8,
                Fill = null,
                Name = "Standard",
            };
            var _lineSeries2 = new LineSeries<double>
            {
                Values = LowerLimit,
                Stroke = new LiveChartsCore.SkiaSharpView.Painting.SolidColorPaint(_red, 2),
                GeometryStroke = new LiveChartsCore.SkiaSharpView.Painting.SolidColorPaint(_red, 2),
                LineSmoothness = 0,
                GeometrySize = 8,
                Fill = null,
                Name = "Lower limit",
            };
            Series.Add(_lineSeries);
            Series.Add(_lineSeries1);
            Series.Add(_lineSeries2);
            Home.Trend.Clear();
            for (int k = 0; k < _configStatic.SCM.StationNumber; k++)
            {
                for (int i = 0; i < _configStatic.SCM.PumpNumber; i++)
                {
                    if (_configStatic.SCM.PumpNumber == _configStatic.SCM.CupNumber)
                    {
                        var model = new TrendModel()
                        {
                            Name = $"{Encoding.ASCII.GetString(new byte[] { (byte)(65 + k) })}_{i + 1}_{i + 1}",
                            InjectionVolumeCollection = new System.Collections.ObjectModel.ObservableCollection<double>(),
                            ProcessCapability = new ProcessCapabilityModel(),
                        };
                        StationNoList.Add(model.Name);
                        Home.Trend.Add(model);
                        Series.Add(new LineSeries<double>
                        {
                            Values = model.InjectionVolumeCollection,

                            GeometryStroke = null,
                            GeometryFill = null,
                            Stroke = new LiveChartsCore.SkiaSharpView.Painting.SolidColorPaint
                            {
                                Color = sKColors[new Random().Next(0, sKColors.Length)],
                                StrokeCap = SkiaSharp.SKStrokeCap.Round,
                                StrokeThickness = 1,
                            },
                            LineSmoothness = 0,
                            GeometrySize = 0,
                            Fill = null,
                            Name = model.Name,
                        });
                        continue;
                    }
                    for (int j = 0; j < _configStatic.SCM.PumpNumber; j++)
                    {
                        var model = new TrendModel()
                        {
                            Name = $"{Encoding.ASCII.GetString(new byte[] { (byte)(65 + k) })}_{i + 1}_{j + 1 + (i * _configStatic.SCM.PumpNumber)}",
                            InjectionVolumeCollection = new System.Collections.ObjectModel.ObservableCollection<double>(),
                            ProcessCapability = new ProcessCapabilityModel(),
                        };
                        StationNoList.Add(model.Name);
                        Home.Trend.Add(model);
                        Series.Add(new LineSeries<double>
                        {
                            Values = model.InjectionVolumeCollection,

                            GeometryStroke = null,
                            GeometryFill = null,
                            Stroke = new LiveChartsCore.SkiaSharpView.Painting.SolidColorPaint
                            {
                                Color = sKColors[new Random().Next(0, sKColors.Length)],
                                StrokeCap = SkiaSharp.SKStrokeCap.Round,
                                StrokeThickness = 1,
                            },
                            LineSmoothness = 0,
                            GeometrySize = 0,
                            Fill = null,
                            Name = model.Name,
                        });
                    }
                }
            }

            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    var model = Home.Trend.FirstOrDefault(x => x.Name == $"A_{i + 1}_{j + 1 + (i * 2)}");
                    if (model == null)
                    {
                        continue;
                    }
                    for (int k = 0; k < 50; k++)
                    {
                        model.InjectionVolumeCollection.Add(new Random().Next(292, 308));
                    }
                }
            }
        }

        public void Load()
        {
            UpperLimit.Clear();
            StandardValue.Clear();
            LowerLimit.Clear();
            for (int i = 0; i < 50; i++)
            {
                UpperLimit.Add(_temporary.InjectionVolumeUP);
                StandardValue.Add(_temporary.InjectionVolume);
                LowerLimit.Add(_temporary.InjectionVolumeLower);
            }
            YAxes[0].MaxLimit = _temporary.InjectionVolumeUP + 1;
            YAxes[0].MinLimit = _temporary.InjectionVolumeLower - 1;
        }

        public void UnLoad()
        {

        }
    }
}
